<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="公告标题" prop="title">
          <el-input v-model="queryParams.title" placeholder="关键字" clearable @keyup.enter="handleQuery()" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery()">
            <template #icon>
              <Search />
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery()">
            <template #icon>
              <Refresh />
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="flex-x-between mb-10px">
        <div>
          <el-button v-hasPerm="['sys:user:add']" type="success" icon="plus" @click="handleOpenDialog()">
            新增
          </el-button>
          <!-- :disabled="selectIds.length === 0" -->
          <el-button v-hasPerm="'sys:user:delete'" type="danger" icon="delete" @click="handleDelete()">
            删除
          </el-button>
        </div>
      </div>
      <el-table ref="dataTableRef" v-loading="loading" :data="pageData" highlight-current-row>
        <el-table-column label="抄表员id" prop="id" width="100" />
        <el-table-column label="姓名" prop="name" width="100" />
        <el-table-column label="手机号" prop="tel" min-width="200" />
        <el-table-column align="center" fixed="right" label="操作" width="120">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="viewNoticeDetail(scope.row.id)">
              修改
            </el-button>
            <el-button type="primary" size="small" link @click="viewNoticeDetail(scope.row.id)">
              修改密码
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="handleQuery()" />
    </el-card>

    <!-- 用户表单 -->
    <el-drawer v-model="dialog.visible" :title="dialog.title" append-to-body @close="handleCloseDialog">
      <el-form ref="userFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="抄表员姓名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item label="手机号" prop="tel">
          <el-input v-model="formData.tel" placeholder="请输入用户昵称" />
        </el-form-item>

        <el-form-item label="登录密码" prop="login_secret">
          <el-input v-model="formData.login_secret" placeholder="请输入登录密码" maxlength="11" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "MyNotice",
  inheritAttrs: false,
});

import NoticeAPI, { NoticePageVO, NoticePageQuery } from "@/api/system/notice";
import UserAPI, { UserForm, UserPageQuery, UserPageVO } from "@/api/system/user";

const queryFormRef = ref(ElForm);
const noticeDetailRef = ref();

const pageData = ref<NoticePageVO[]>([]);

const loading = ref(false);
const total = ref(0);

const queryParams = reactive<NoticePageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const dialog = reactive({
  visible: false,
  title: "新增用户",
});


const formData = reactive<UserForm>({
  status: 1,
});

const rules = reactive({
  username: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
  nickname: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
  deptId: [{ required: true, message: "所属部门不能为空", trigger: "blur" }],
  roleIds: [{ required: true, message: "用户角色不能为空", trigger: "blur" }],
});

/**
 * 打开弹窗
 *
 * @param id 用户ID
 */
async function handleOpenDialog(id?: number) {
  dialog.visible = true;

  if (id) {
    dialog.title = "修改用户";
  } else {
    dialog.title = "新增用户";
  }
}

const userFormRef = ref(ElForm);

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  userFormRef.value.resetFields();
  userFormRef.value.clearValidate();

  formData.id = undefined;
  formData.status = 1;
}

const handleSubmit = () => {

}

const handleDelete = () => {

}

/** 查询通知公告 */
function handleQuery() {
  loading.value = true;
  NoticeAPI.getMyNoticePage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置通知公告查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

function viewNoticeDetail(id: string) {
  // noticeDetailRef.value.openNotice(id);
}

onMounted(() => {
  handleQuery();
});
</script>
